package gameoflife.clips;
import gameoflife.main.Game;

import javax.swing.SwingWorker;
import javax.swing.table.TableModel;

import CLIPSJNI.Environment;

public class ClipsEvalProcess extends SwingWorker<Void, Void>{

	private boolean[][] grid = new boolean[Game.SIZE][Game.SIZE];
	private TableModel tm;
	Environment clipsenv;
	
	public ClipsEvalProcess(TableModel tm, Environment clipsenv) {
		this.tm = tm;
		this.clipsenv = clipsenv;
	}
	
	@Override
	protected Void doInBackground() throws Exception {
		for (int i = 0; i < tm.getRowCount(); i++) {
			for (int j = 0; j < tm.getColumnCount(); j++) {
				grid[i][j] = (Boolean)tm.getValueAt(i, j);
			}
		}
		ClipsProcesses.initAsserts(clipsenv, grid);

		clipsenv.run();

		grid = ClipsProcesses.getResult(clipsenv);
		
		clipsenv.reset();
		return null;
	}
	
	@Override
	protected void done() {
		for (int i = 0; i < grid.length; i++) {
			for (int j = 0; j < grid[0].length; j++) {
				tm.setValueAt(grid[i][j], i, j);
			}
		}
	}

}
